package com.mibao.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.mibao.db.DBAdapter;
import com.mipai.R;

/**
 * @todo 修改密码
 */
public class SetPwdActivity extends Activity {
	private ImageButton backButton = null;
	private Button switchButton = null;
	private String switchStr = "on";
	private Button sureButton = null;
	private Button cancelButton = null;
	private EditText newpwd = null;
	private EditText renewpwd = null;
	private EditText oldpwd = null;
	private TextView pwd_tip = null;
	private String password = "";
	//private SharedPreferences sharedPre = getSharedPreferences("first_pref", Context.MODE_PRIVATE);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setpwd);
		newpwd = (EditText) findViewById(R.id.newpwd);
		renewpwd = (EditText) findViewById(R.id.renewpwd);
		oldpwd = (EditText) findViewById(R.id.oldpwd);
		pwd_tip = (TextView) findViewById(R.id.pwd_tip);

		backButton = (ImageButton) findViewById(R.id.backButton);
		backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(SetPwdActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		});

		// 绑定摇一摇事件
		switchButton = (Button) findViewById(R.id.btn_yao_switch);
		switchButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (switchStr.equals("on")) {
					((Button) view).setBackground(getResources().getDrawable(R.drawable.switch_off));
					switchStr = "off";
				} else {
					((Button) view).setBackground(getResources().getDrawable(R.drawable.switch_on));
					switchStr = "on";
				}
				
			}
		});

		// 确认密码修改事件
		sureButton = (Button) findViewById(R.id.btn_sure);
		sureButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				pwd_tip = (TextView) findViewById(R.id.pwd_tip);
				pwd_tip.setText("");
				String newPwdStr = newpwd.getText().toString();
				String reNewPwdStr = renewpwd.getText().toString();
				String oldPwdStr = oldpwd.getText().toString().trim();
				if (!newPwdStr.equals("") && !reNewPwdStr.equals("")) {
					if (oldPwdStr.equals(password)) {
						if (newPwdStr.equals(reNewPwdStr)) {
							// MainActivity.password=reNewPwdStr;
							DBAdapter dao = new DBAdapter(SetPwdActivity.this).open();
							password = reNewPwdStr;
							boolean flag = dao.insertPwd(reNewPwdStr);
							if (flag) {
								pwd_tip.setText("密码修改成功！");
							} else {
								pwd_tip.setText("密码修改失败！");
							}
							dao.close();
						} else {
							pwd_tip.setText("重复密码错误，请重新输入密码！");
						}
					} else {
						pwd_tip.setText("原密码错误，请重新输入密码！");
					}
				}
			}
		});

		// 确认按钮监听器
		sureButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					view.setBackgroundResource(R.drawable.sure_on);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					view.setBackgroundResource(R.drawable.sure_off);
				}
				return false;
			}
		});

		// 密码取消事件
		cancelButton = (Button) findViewById(R.id.btn_cancel);
		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				TextView pwd_tip = (TextView) findViewById(R.id.pwd_tip);
				pwd_tip.setText("");

				TextView newpwd = (TextView) findViewById(R.id.newpwd);
				newpwd.setText("");
				TextView renewpwd = (TextView) findViewById(R.id.renewpwd);
				renewpwd.setText("");
			}
		});

		cancelButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					view.setBackgroundResource(R.drawable.cancel_on);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					view.setBackgroundResource(R.drawable.cancel_off);
				}
				return false;
			}
		});
	}

}
